import redis


class Myredis:
    def __init__(self):
        self.pool = redis.ConnectionPool(host='localhost', port=6379, db=2, password='123456')
        self.coon = redis.Redis(connection_pool=self.pool)

    def str_set(self, key, value):
        self.coon.set(key, value)

    def str_get(self, key):
        return self.coon.get(key)

    def img_code_set(self, key, time, value):
        self.coon.setex(key, time, value)

    def l_push(self, key, value):
        self.coon.lpush(key, value)

    def r_pop(self, key):
        return self.coon.rpop(key)

    def t_len(self, key):
        return self.coon.llen(key)

    def lis_lrem(self, name, count, value):
        return self.coon.lrem(name, count, value)

    # hash
    # 存入数据
    def hset(self, pkey, key, v):
        self.coon.hset(pkey, key, v)

    # 获取单个数据
    def hget(self, pkey, key):
        return self.coon.hget(pkey, key)

    # 获取所有数据
    def hgetall(self, pkey):
        return self.coon.hgetall(pkey)

    # 存入数据不覆盖
    def hsetnx(self, pkey, key, v):
        self.coon.hsetnx(pkey, key, v)

    # 加减数量
    def hash_jia_jian(self, pkey, key, count):
        self.coon.hincrby(pkey, key, count)

    # h_setnx
    def str_setnx(self, key, v):
        return self.coon.setnx(key, v)

    def store_change(self, key, count, type):
        # type 1加  2减
        if type == 1:
            self.coon.decrby(key, count)
        else:
            self.coon.incrby(key, count)

    def h_del(self, pkey, key):
        return self.coon.hdel(pkey, key)

    # set
    def set_add(self, k, v):
        self.coon.sadd(k, v)

    def set_getall(self, k):
        return self.coon.smembers(k)

    def set_del(self, k):
        return self.coon.delete(k)

    # 有序集合;zset
    # 添加 时间戳（score） 订单号(order) {order:socre}
    def zset_add(self, key, score, value):
        mapping = {value: score}
        self.coon.zadd(key, mapping)

    # 区间查询
    def zset_range_getall(self, key, max):
        return self.coon.zrangebyscore(key, 0, max)

    # 删除
    def zset_del(self, key, value):
        return self.coon.zrem(key, value)


myredis = Myredis()
